import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Goods from '../views/Goods.vue'
// import Goods from '../views/GoodsBackup.vue'
import Details from '../views/Details.vue'
import Orders from '../views/Orders.vue'
import ShoppingCart from '../views/ShoppingCart.vue'
import Collect from '../views/Collect.vue'
import ConfirmOrder from '../views/ConfirmOrder.vue'
import MyError from '../components/MyError.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
/*    {
        path: '/goods',
        name: 'Goods',
        component: Goods
    },*/
    {
        path: '/goods',
        name: 'Goods',
        component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
    },
    {
        path: '/details',
        name: 'Details',
        component: Details
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/error',
        name: 'Error',
        // component: () => import(/* webpackChunkName: "error" */ '../components/MyError.vue')
        component: MyError
    },
    {
        path: '/orders',
        name: 'Orders',
        component: Orders,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/shoppingCart',
        name: 'ShoppingCart',
        component: ShoppingCart,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/collect',
        name: 'Collect',
        component: Collect,
        meta: {
            requireAuth: true
        }
    },
    {
        path: '/confirmOrder',
        name: 'ConfirmOrder',
        component: ConfirmOrder,
        meta: {
            requireAuth: true
        }
    },
];



//控制台会报[NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}]。其原因在于Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数，错误信息就会交给全局的路由错误处理，因此就会报上述的错误。
//禁止全局路由错误处理打印，这个也是vue-router开发者给出的解决方案：
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
    routes
});

import store from "../store"

//路由守卫
router.beforeEach((to, from, next)=>{
    // console.log("path====>",to.path);
    // console.log("meta====>",to.meta);
    let userInfo = window.localStorage.getItem("user");
    if(to.meta.requireAuth){//如果requireAuth存在,需要页面鉴权
        if(!userInfo){
            // 没有登录，显示登录组件
            store.dispatch("setShowLogin", true);
            next(false);
            return
        }
    }
    next()

    /*    if(/(order|shoppingCart|collect|confirmOrder)/.test(to.path)){

     }*/


});

export default router
